//
//  HMVisitorView.swift
//  Weibo
//
//  Created by mac on 2017/6/5.
//  Copyright © 2017年 mac. All rights reserved.
//

import UIKit
import SnapKit
//定义代理协议
protocol  HMVisitorViewDelegate:NSObjectProtocol{
    //声明登录操作的函数
    func loginOperation()
}

//自定义访客视图
class HMVisitorView :UIView{
    
    //接受可以执行登录的闭包
    var loginCallBack:(()->())?
    
    //接受设置的代理
    weak var delegate: HMVisitorViewDelegate?
    //旋转视图
    fileprivate lazy var cylceImageView:UIImageView=UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    //罩层图片
    fileprivate lazy var maskiconImageView:UIImageView=UIImageView(image: UIImage (named:"visitordiscover_feed_mask_smallicon"))
    //主页图片
   fileprivate lazy var homeIconImageView:UIImageView=UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    //消息文本
    fileprivate lazy var messaryLabel:UILabel={
        let label=UILabel()
        label.font=UIFont.systemFont(ofSize: 13)
        label.textColor=UIColor.darkGray
        label.numberOfLines=0
        label.text="关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .center
        return label
        
        
    }()
    //注册
    fileprivate lazy var registerButton:UIButton={
        let button = UIButton()
        //点击事件
        button.addTarget(self, action: #selector(registerButtonAction), for: UIControlEvents.touchUpInside)
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font=UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        let image=UIImage(named:"common_button_white_disable")!
        //根据四边拉伸图片
        let stretchImage = image.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5),resizingMode:.stretch)

        //设置背景图片
        button.setBackgroundImage(stretchImage, for: .normal)
        //去掉高亮状态
        button.adjustsImageWhenHighlighted=false
                return button
    }()
    //登录
    fileprivate lazy var loginButton:UIButton={
        let button = UIButton()
        //点击事件
        button.addTarget(self, action: #selector(loginButtonAction), for: UIControlEvents.touchUpInside)
        button.setTitle("登录", for: .normal)
        button.titleLabel?.font=UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        let image=UIImage(named:"common_button_white_disable")!
        //根据四边拉伸图片
        let stretchImage = image.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5),resizingMode:.stretch)
        
        //设置背景图片
        button.setBackgroundImage(stretchImage, for: .normal)
        //去掉高亮状态
        button.adjustsImageWhenHighlighted=false
        return button
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加控件设置约束
    private func setupUI(){
        backgroundColor=UIColor(white: 237 / 255, alpha: 1)
        
        //添加控件
        addSubview(cylceImageView)
        addSubview(maskiconImageView)
        addSubview(homeIconImageView)
        addSubview(messaryLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        cylceImageView.snp.makeConstraints{(make)in
            make.center.equalTo(self)
            
        }
        maskiconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cylceImageView)
        }
        homeIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cylceImageView)
        }
        messaryLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cylceImageView.snp.bottom)
            make.centerX.equalTo(cylceImageView)
            make.width.equalTo(224)
        }
        registerButton.snp.makeConstraints { (make) in
            make.leading.equalTo(messaryLabel)
            make.top.equalTo(messaryLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(messaryLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
    }
    //开启旋转视图动画
    private func startAnimation(){
        //创建基本动画对象
        let animation = CABasicAnimation(keyPath:"transform.rotation")
        //设置动画的目的地
        animation.toValue=2*Double.pi
        //设置时长
        animation.duration=20
        //次数,最大值
        animation.repeatCount=MAXFLOAT
        //切换页面的时候不让其删除动画
        animation.isRemovedOnCompletion=false
        //动画要layer执行
        cylceImageView.layer.add(animation, forKey: nil)
        
    }
    //修改访客视图的信息
    func updateVisitorViewInFo(imageName:String?,message:String?) {
        if let imageName=imageName,let message=message{
            //不是首页
            homeIconImageView.image=UIImage(named:imageName)
            messaryLabel.text=message
            //隐藏旋转视图
            cylceImageView.isHidden=true
        }else{
            //是首页
            startAnimation()
        }
    }
    //登录注册按钮事件处理
    @objc private func registerButtonAction(){
        
        //执行闭包完成回调
        loginCallBack?()
        //通过代理执行代理方法完成回调
        delegate?.loginOperation()
    }

    @objc private func loginButtonAction(){
        //执行闭包完成回调
        loginCallBack?()
        //通过代理执行代理方法完成回调
        delegate?.loginOperation()
    }
    
}
extension HMVisitorView{
    func test(){
    //手写代理添加autolayout->以后自己封装控件,需要手写代码进行布局
        cylceImageView.translatesAutoresizingMaskIntoConstraints=false
        //创建水平centerx的约束
        let cycleCenterX=NSLayoutConstraint(item: cylceImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
        let cycleCenterY=NSLayoutConstraint(item: cylceImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
        //添加约束
        addConstraint(cycleCenterX)
        addConstraint(cycleCenterY)
    }
}
